#include "applauncher.h"
#include "countdown.h"
#include "global.h"
bool liveMode = false;
bool isInClass = 0;
double classProgressPercentage = 0.0;
QString currentSubject = "";
QScreen *g_primaryScreen = nullptr;
QRect g_screenGeometry;
QRect g_availableGeometry;
CurrentClassInfo g_currentClassInfo;
void initScreenCache()
{
	QList<QScreen *> screens = QGuiApplication::screens();
	int screenCount = screens.size();
	if (screenCount > 1) {
		QStringList screenNames;
		for (QScreen *screen : screens) {
			QString desc = QString("%1 (%2x%3)").arg(screen->name()).arg(screen->size().width()).arg(screen->size().height());
			screenNames << desc;
		}
		bool ok;
		QString selection = QInputDialog::getItem(nullptr, "教室多媒体", "检测到多个显示器，请选择:", screenNames, 0, false, &ok);
		if (ok && !selection.isEmpty()) {
			int index = screenNames.indexOf(selection);
			if (index >= 0 && index < screens.size()) g_primaryScreen = screens.at(index);
		}
	}
	if (!g_primaryScreen) g_primaryScreen = screenCount > 0 ? screens.first() : QGuiApplication::primaryScreen();
	if (g_primaryScreen) {
		g_screenGeometry = g_primaryScreen->geometry();
		g_availableGeometry = g_primaryScreen->availableGeometry();
	}
}
constexpr const char *Config::Keys::BARRAGE;
constexpr const char *Config::Keys::SCREENSAVER;
constexpr const char *Config::Keys::HIDDEN_SUBJECTS;
constexpr const char *Config::Keys::GANFAN_TIMES;
constexpr const char *Config::Keys::SHUTDOWN_TIMES;
constexpr const char *Config::Keys::ENROLLMENT_TIMESTAMP;
constexpr const char *Config::Keys::GAOKAO_TIMESTAMP;
Config &Config::instance()
{
	static Config instance;
	return instance;
}
Config::Config(QObject *parent) : QObject(parent)
{
	QDir().mkpath(configDir);
	m_settings = new QSettings(configDir + "/config.ini", QSettings::IniFormat, this);
	loadConfig();
}
void Config::loadConfig()
{
	m_barrage = m_settings->value(Keys::BARRAGE, true).toBool();
	m_screensaver = m_settings->value(Keys::SCREENSAVER, true).toBool();
	m_hiddenSubjects = m_settings->value(Keys::HIDDEN_SUBJECTS, "").toString();
	m_ganfanTimes = m_settings->value(Keys::GANFAN_TIMES, "75,90").toString().split(',', Qt::SkipEmptyParts);
	m_shutdownTimes = m_settings->value(Keys::SHUTDOWN_TIMES, "0,60").toString().split(',', Qt::SkipEmptyParts);
	m_enrollmentTimestamp = m_settings->value(Keys::ENROLLMENT_TIMESTAMP, QDateTime(QDate(2023, 8, 17), QTime(18, 50)).toSecsSinceEpoch()).toLongLong();
	m_gaokaoTimestamp = m_settings->value(Keys::GAOKAO_TIMESTAMP, 1780794000).toLongLong();
}
void Config::saveConfig()
{
	m_settings->setValue(Keys::BARRAGE, m_barrage);
	m_settings->setValue(Keys::SCREENSAVER, m_screensaver);
	m_settings->setValue(Keys::HIDDEN_SUBJECTS, m_hiddenSubjects);
	m_settings->setValue(Keys::GANFAN_TIMES, m_ganfanTimes.join(','));
	m_settings->setValue(Keys::SHUTDOWN_TIMES, m_shutdownTimes.join(','));
	m_settings->setValue(Keys::ENROLLMENT_TIMESTAMP, m_enrollmentTimestamp);
	m_settings->setValue(Keys::GAOKAO_TIMESTAMP, m_gaokaoTimestamp);
	m_settings->sync();
}
int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	for (int i = 1; i < argc; ++i) {
		if (QString(argv[i]) == "--live" || QString(argv[i]) == "-l") {
			liveMode = true;
			break;
		}
	}
	QString appName = liveMode ? "classroom_computer_live_h890R/&F]9Ntghl^YU(" : "classroom_computer_h890R/&F]9Ntghl^YU(";
	QDir().mkpath(configDir);
	QDir::setCurrent(configDir);
	QFont defaultFont(MAINFT);
	defaultFont.setStyleStrategy(QFont::NoAntialias);
	a.setFont(defaultFont);
	initScreenCache();
	CountDown w1;
	AppLauncher w2;
	QTimer classInfoTimer;
	QObject::connect(&classInfoTimer, &QTimer::timeout, [&w2]() { g_currentClassInfo = w2.getCurrentClassInfo(); });
	classInfoTimer.start(3000);
	w1.show();
	w2.show();
	return a.exec();
}
